<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品分类" prop="goodType">
      <el-input v-model="dataForm.goodType" placeholder="商品分类"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="goodImg">
      <el-input v-model="dataForm.goodImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品状态(1显示 2不显示)" prop="goodShowStatus">
      <el-input v-model="dataForm.goodShowStatus" placeholder="商品状态(1显示 2不显示)"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="goodSort">
      <el-input v-model="dataForm.goodSort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
          baseURL: 'http://localhost:7001',
        visible: false,
        dataForm: {
          id: 0,
          goodType: '',
          goodImg: '',
          goodShowStatus: '',
          goodSort: ''
        },
        dataRule: {
          goodType: [
            { required: true, message: '商品分类不能为空', trigger: 'blur' }
          ],
          goodImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          goodShowStatus: [
            { required: true, message: '商品状态(1显示 2不显示)不能为空', trigger: 'blur' }
          ],
          goodSort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
              axios({
              url: `${this.baseURL}/hospital_goods/goodtype/info/${this.dataForm.id}`,
              method: 'get',
              params: '',
              // `responseType` 表示浏览器将要响应的数据类型
              // 选项包括: 'arraybuffer', 'document', 'json', 'text', 'stream'
              responseType: 'json'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodType = data.goodType.goodType
                this.dataForm.goodImg = data.goodType.goodImg
                this.dataForm.goodShowStatus = data.goodType.goodShowStatus
                this.dataForm.goodSort = data.goodType.goodSort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            axios({
              url: `${this.baseURL}/hospital_goods/goodtype/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'goodType': this.dataForm.goodType,
                'goodImg': this.dataForm.goodImg,
                'goodShowStatus': this.dataForm.goodShowStatus,
                'goodSort': this.dataForm.goodSort
              },
            // `responseType` 表示浏览器将要响应的数据类型
            // 选项包括: 'arraybuffer', 'document', 'json', 'text', 'stream'
            responseType: 'json'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
